// app.js
class AppBootHook {
  constructor(app) {
    this.app = app;
  }

  configWillLoad() {}

  async didLoad() {}

  async willReady() {}

  async didReady() {
    // 应用已经启动完毕
    // 更新redis中的数据
    const ctx = await this.app.createAnonymousContext();
    await ctx.service.tool.redis.refreshRedis();

    //启动定时任务
    await ctx.service.monitor.sysJob.scheduleStart();

    //获取ip
    await ctx.service.getIp.setIp();

    //监听agent
    this.app.messenger.on("executeServiceMethod", async ({ taskKey }) => {
      let errCode = "";
      let res = "";
      let task = taskKey;
      try {
        res = await eval(`ctx.service.${task.invoke_target}`);
      } catch (error) {
        errCode = error.message;
      }
      await ctx.service.monitor.sysJobLog.create({
        job_name: task.job_name,
        job_group: task.job_group,
        invoke_target: task.invoke_target,
        job_message: JSON.stringify(res),
        status: errCode ? "1" : "0",
        exception_info: errCode,
        create_time: new Date(),
      });
    });
  }

  async serverDidReady() {}
}

module.exports = AppBootHook;
